package com.charles.seckillPlatform.service.impl;

import cn.hutool.core.util.IdUtil;
import com.charles.seckillComponentCommon.constants.CacheConstants;
import com.charles.seckillComponentCommon.domain.TableInfo;
import com.charles.seckillComponentCommon.utils.PageUtils;
import com.charles.seckillComponentRedis.service.RedisService;
import com.charles.seckillData.domain.SysGood;
import com.charles.seckillData.domain.SysLog;
import com.charles.seckillData.domain.SysOrder;
import com.charles.seckillPlatform.mapper.SysGoodMapper;
import com.charles.seckillPlatform.mapper.SysLogMapper;
import com.charles.seckillPlatform.mapper.SysOrderMapper;
import com.charles.seckillPlatform.service.SysOrderService;
import com.charles.seckillPlatform.vo.OrderListVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class SysOrderServiceImpl implements SysOrderService {

    @Autowired
    private SysOrderMapper sysOrderMapper;

    @Autowired
    private SysGoodMapper sysGoodMapper;

    @Autowired
    private SysLogMapper sysLogMapper;

    @Autowired
    private RedisService redisService;

    /**
     * 因为官方给的PageHelper满足不了我们的需求，因此这边自己手动进行了分页
     *
     * @param userId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public TableInfo queryOrderList(Long userId, Integer pageNum, Integer pageSize) {
        List<OrderListVo> orderList = sysOrderMapper.queryOrderList(userId);

        for (SysGood good : sysGoodMapper.queryGoodList()) {
            String orderId = (String) redisService.getCacheObject(CacheConstants.STORE_ORDER_ID + userId + good.getGood_id());
            if (orderId != null && !orderId.isEmpty()) {
                SysOrder order = (SysOrder) redisService.getCacheObject(CacheConstants.ORDER_ID + orderId);
                if (order != null) {
                    OrderListVo orderListVo = new OrderListVo();
                    orderListVo.setOrderId(orderId);
                    orderListVo.setGoodTitle(order.getGood_title());
                    orderListVo.setPayPrice(Optional.ofNullable(order.getSeckill_price()).orElse(order.getPrice()));
                    orderListVo.setStatus(order.getStatus());
                    orderListVo.setCreateTime(order.getCreate_time());
                    orderList.add(orderListVo);
                }
            }
        }

        List<OrderListVo> res = (List<OrderListVo>) PageUtils.startPage(orderList, pageNum, pageSize);
        TableInfo rspData = new TableInfo();
        rspData.setCode(200);
        rspData.setRows(res);
        rspData.setMsg("查询成功");
        rspData.setTotal(new PageInfo(orderList).getTotal());
        return rspData;
    }

    @Override
    public SysOrder getOrder(String orderId) {
        SysOrder order = (SysOrder) redisService.getCacheObject(CacheConstants.ORDER_ID + orderId);
        if (order == null) {
            order = sysOrderMapper.queryOrder(orderId);
        }

        return order;
    }

    // 支付成功的回调
    @Override
    public void payOrderSuccess(String orderId, String payWay) {
        SysOrder order = redisService.getCacheObject(CacheConstants.ORDER_ID + orderId);

        if (!order.getStatus().equals("1")) {
            // 修改订单状态
            order.setStatus("1");
            order.setPay_way(payWay);
            order.setPay_time(new Date());
            redisService.setCacheObject(CacheConstants.ORDER_ID + orderId, order);

            // 存入日志
            SysLog log = new SysLog();
            log.setLog_id(IdUtil.fastSimpleUUID());
            log.setUser_id(order.getUser_id());
            log.setLog_type("order");
            log.setMsg("用户id:" + order.getUser_id() + "支付了订单:" + orderId);
            sysLogMapper.addLog(log);
        }
    }

    @Override
    public boolean checkOrderIsFinished(String orderId) {
        SysOrder order = redisService.getCacheObject(CacheConstants.ORDER_ID + orderId);
        
        return order.getStatus().equals("1");
    }
}
